﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Csla;
using Csla.Data;

namespace Aiss.Sync
{
    [Serializable]
    public class SyncInfo:BusinessBase<SyncInfo>
    {
        int iD = 0;
        string xml = "";
        string description = "";

        public int ID
        {
            get { return iD; }
        }

        public string Xml
        {
            get { return xml; }
            set 
            {
                if (xml.Equals(value) == false)
                {
                    xml = value;
                    PropertyHasChanged("Xml");
                }
            }
        }

        public string Description
        {
            get { return description; }
            set
            {
                if (description.Equals(value) == false)
                {
                    description = value;
                    PropertyHasChanged("Xml");
                }
            }
        }

        private SyncInfo()
        {
        }

        private class Criteria
        {
        }

        public static SyncInfo NewSyncInfo()
        {
            return DataPortal.Create<SyncInfo>(new Criteria());
        }

        public static SyncInfo GetSyncInfo()
        {
            return DataPortal.Fetch<SyncInfo>(new Criteria());
        }


        protected override void DataPortal_Fetch(object criteria)
        {
            using (var mgr = Csla.Data.ContextManager<DALRetail.RetailDataContext>.GetManager(Settings.ConnStringMain, false))
            {
                var a = (from i in mgr.DataContext.SyncInfos select i).FirstOrDefault();
                if (a == null)
                    return;
                this.iD = a.ID;
                this.xml = a.XML;
                this.description = a.Description;

                MarkOld();
            }
        }

        protected override void DataPortal_Insert()
        {
            using (var mgr = Csla.Data.ContextManager<DALRetail.RetailDataContext>.GetManager(Settings.ConnStringMain, false))
            {
                int? newID = 0;
                mgr.DataContext.AddSyncInfo(this.xml,this.description,ref newID);
                this.iD = newID ?? 0;

                MarkOld();
            }
        }

        protected override void DataPortal_Update()
        {
            using (var mgr = Csla.Data.ContextManager<DALRetail.RetailDataContext>.GetManager(Settings.ConnStringMain, false))
            {
                mgr.DataContext.UpdateSyncInfo(this.iD,this.xml, this.description);

                MarkOld();
            }
        }
        
    }
}
